package interchange.data;

import interchange.domain.Login;
import interchange.domain.Pago;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCPagoDAO implements IPagoDAO {
	
	public void delete(Connection conn, String IDPago){
		String sql = "DELETE FROM Pago WHERE (OIDPago = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, IDPago);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }

	}
    public void insert(Connection conn, String IDPago, Pago p){
    	 PreparedStatement stmt = null;
         String sql = "INSERT INTO Pago ( OIDPago, formaDePago) VALUES (?, ?) ";

         try {
             stmt = conn.prepareStatement(sql);

             stmt.setString(1, IDPago);
             stmt.setString(2, p.getFormaDePago());
             

             stmt.executeUpdate();

         } catch (SQLException e) {
             System.out.println("Message: " + e.getMessage());
             System.out.println("SQLState: " + e.getSQLState());
             System.out.println("ErrorCode: " + e.getErrorCode());
         } finally {
             try {
                 if (stmt != null) {
                     stmt.close();
                 }
             } catch (SQLException e) {
             }
         }

    }
   
    public Pago select(Connection conn,String formaDePago){
    	String sql = "SELECT * FROM Pago WHERE (OIDPago = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Pago p=null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, formaDePago);

            result = stmt.executeQuery();

            result.next();
            
            
            p = new Pago();
            p.setFormaDePago(result.getString("formaDePago"));
     
            
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return p;
    }
public void update(Connection conn,String IDPagoAnt,String IDPago,String formaDePago) {
		
		
		String sql = "UPDATE Pago SET  OIDPago=?, formaDePago=? WHERE (OIDPago=?)";
		PreparedStatement stmt = null;
		
		try {
            stmt = conn.prepareStatement(sql);

           
            stmt.setString(1, IDPago);
            stmt.setString(2, formaDePago);
            stmt.setString(3, IDPagoAnt);
          
            

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		

	}

	

    

}


